package com.jing.shop.service.serviceImpl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jing.common.core.domain.R;
import com.jing.common.core.utils.FastUtil;
import com.jing.shop.domain.dto.ProductBrandDTO;
import com.jing.shop.domain.po.ProductBrandPO;
import com.jing.shop.domain.vo.ProductBrandVO;
import com.jing.shop.mapper.ProductBrandMapper;
import com.jing.shop.service.ProductBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class ProductBrandServiceImpl implements ProductBrandService {

    @Autowired
    private ProductBrandMapper productBrandMapper;
    @Autowired
    private FastUtil fastUtil;

    @Override
    public PageInfo<ProductBrandPO> findProductBrandList(ProductBrandDTO productBrandDTO) {
        PageHelper.startPage(productBrandDTO.getPageNum(),productBrandDTO.getPageSize());
        List<ProductBrandPO> productBrandList = productBrandMapper.findProductBrandList(productBrandDTO);
        PageInfo<ProductBrandPO> productBrandPOPageInfo = new PageInfo<>(productBrandList);
        return productBrandPOPageInfo;
    }

    @Override
    public void addBrand(ProductBrandDTO productBrandDTO) {
        Integer flag = productBrandMapper.addBrand(productBrandDTO);
        if(flag<=0){
            throw new RuntimeException("添加异常");
        }
    }

    @Override
    public void updateBrand(ProductBrandDTO productBrandDTO) {
        Integer flag = productBrandMapper.updateBrand(productBrandDTO);
        if(flag<=0){
            throw new RuntimeException("修改异常");
        }
    }

    @Override
    public void delBrandIds(Integer[] ids) {
        Integer flag = productBrandMapper.delBrandIds(ids);
        if(flag<=0){
            throw new RuntimeException("批量删除异常");
        }
    }

    @Override
    public void delBrandById(Integer brandId) {
        Integer flag = productBrandMapper.delBrandById(brandId);
        if(flag<=0){
            throw new RuntimeException("删除异常");
        }
    }

    @Override
    public R upload(MultipartFile multipartFile) {
        String url="";
        try {
            url = fastUtil.upload(multipartFile);
            if(url.equals("")){
                return R.fail("上传图片失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok("http://106.54.45.36:8888/"+url);
    }

    @Override
    public void updatebrandFactory(ProductBrandDTO productBrandDTO) {
        Integer flag = productBrandMapper.updatebrandFactory(productBrandDTO);
        if(flag<=0){
            throw new RuntimeException("品牌制造商异常");
        }
    }

    @Override
    public void updatebrandShow(ProductBrandDTO productBrandDTO) {
        Integer flag = productBrandMapper.updatebrandShow(productBrandDTO);
        if(flag<=0){
            throw new RuntimeException("是否显示异常");
        }
    }

    @Override
    public List<ProductBrandVO> findBrandList() {
        List brandList = productBrandMapper.findBrandList();
        return brandList;
    }
}
